package utils;

import java.util.List;

import geo.google.GeoAddressStandardizer;
import geo.google.GeoException;
import geo.google.datamodel.GeoAddress;
import geo.google.datamodel.GeoCoordinate;
import models.Address;

/**
 * Geo co-ordinate related helper class
 * 
 * @author Deepak
 * 
 */
public final class GeoAddressUtil {

	public static final String GOOGLE_API_KEY = "AIzaSyBTeZwsP1byT0usPDpTyjPvlbOABFXGtDI";

	/**
	 * Helper method to get the geo co-ordinates given an address
	 * 
	 * @param address
	 * @return
	 * @throws GeoException
	 */
	public static final GeoCoordinate getGeoCoordinates(Address address) throws GeoException {
		if (null == address) {
			throw new GeoException("Cannot have a null address");
		}

		StringBuilder sb = new StringBuilder();
		sb.append(address.street);
		sb.append(" ");
		sb.append(address.houseNumber);
		sb.append(" ");
		sb.append(address.zipCode);
		sb.append(" ");
		sb.append(address.city);
		sb.append(" ");
		sb.append("Germany");

		GeoAddressStandardizer st = new GeoAddressStandardizer(GOOGLE_API_KEY);

		// Get a list of possible matching addresses
		List<GeoAddress> addresses = st.standardizeToGeoAddresses(sb.toString());

		// Get the first address
		GeoAddress geoAddress = addresses.get(0);

		// Get the coordinates for the address
		GeoCoordinate coords = geoAddress.getCoordinate();
		return coords;
	}

	public static void main(String[] args) throws GeoException {
		Address addr = new Address();
		addr.city = "Test";
		addr.houseNumber = "33";
		addr.street = "33";
		addr.zipCode = "33";
		System.out.println(getGeoCoordinates(addr));

	}
}
